﻿using Business.Service;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcAppWeb.Controllers
{
    public class DCUserManageController : BaseDataController
    {

        public ActionResult DCUserManage()
        {
            if (Session["UserInfo"] == null) return RedirectToAction("LogOn", "Account");
            else
            {
                AppUserList model = (AppUserList)Session["UserInfo"];
                return View(model);

            }
        }


        ///<summary>
        ///查询前置仓仓库信息
        ///</summary>
        ///<returns></returns>
        public JsonResult InitJsonResult(string warehouseCode, string warehouseName, string userCode, string userPhone, string goodsBatchNo, string status)
        {
            string where = "  where 1=1 ";
            int page = int.Parse(Request["page"].ToString());
            int rows = int.Parse(Request["rows"].ToString());
            int total = 0;
            if (warehouseCode != "")
            {
                where += " and t3.warehouseCode like '%" + warehouseCode + "%'";
            }
            if (warehouseName != "")
            {
                where += " and t3.warehouseName like '%" + warehouseName + "%'";
            }
            if (userCode != "")
            {
                where += " and t1.userCode='" + userCode + "'";
            }
            if (userPhone != "")
            {
                where += " and t1.userPhone='" + userPhone + "'";
            }
            if (status != "")
            {
                where += " and t1.status='" + status + "'";
            }
            var retData = new object();
            BDCUserManage db = new BDCUserManage();
            retData = db.GetDCUserManageInfo(page, rows, out total, where).ToList();
            Dictionary<string, object> json = new Dictionary<string, object>();
            json.Add("total", total);
            json.Add("rows", retData);
            return Json(json);
        }

        /// <summary>
        /// 修改or新增
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string SaveData()
        {
            string sjson = string.Empty;
            try
            {
                AppUserList model = (AppUserList)Session["UserInfo"];
                DCUserManage dm = new DCUserManage();

                string userManageId = Request["hiddenUSERMANAGEID"]; ;

                dm.USERCODE = Request["stxtUserCode"].ToString();
                dm.USERPHONE = Request["txtPhone"].ToString();
                dm.WAREHOUSEID = Convert.ToInt32(Request["sWarehHuseId"]);
                dm.OPRNO = model.USERCODE.ToString();
                dm.STATUS = Request["sstatus"].ToString();

                BDCUserManage db = new BDCUserManage();
                if (dm.WAREHOUSEID == 0)
                {
                     sjson = "{success:false,msg:'必须选择仓库！'}";

                }
                else
                {
                    if (string.IsNullOrEmpty(userManageId))
                    {
                        List<DCUserManage> list = db.GetDCUserManageByCode(dm.USERCODE);
                        if (list.Count > 0)
                        {
                            sjson = "{success:false,msg:'此员工已经绑定仓库！'}";
                        }
                        else
                        {
                            int n = db.AddDCUserManage(dm);

                            if (n > 0)
                            {
                                sjson = "{success:true}";
                            }
                            else
                            {
                                sjson = "{success:false,msg:'添加失败.请联系管理员！'}";
                            }
                        }
                    }
                    else
                    {
                        dm.USERMANAGEID = int.Parse(userManageId);

                        int n = db.UpdateDCUserManage(dm);
                        if (n > 0)
                        {
                            sjson = "{success:true}";
                        }
                        else
                        {
                            sjson = "{success:false,msg:'修改失败.请联系管理员！'}";
                        }
                    }
                }
            


                return sjson;

            }
            catch (Exception ex)
            {
                sjson = "{success:false,msg:'" + ex.Message + "'}";
            }
            return sjson;
        }



    }
}
